﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Data.Objects.SqlClient;
using eq.CoreBase.Control;

namespace JobDescOnline.Presentation.Models
{
    public static class Privileges
    {
        public static JobDescDBEntities1 db = new JobDescDBEntities1();
        public static dynamic RetrieveByRoleID()
        {
            var _temp = (from m in db.Menus
                         join mp in (
                            from mp in db.Menus where mp.Rowstatus == true
                            select mp
                         ) on m.ParentID equals mp.ID into mpo from mpox in mpo.DefaultIfEmpty()
                         join p in (
                            from p in db.Privilleges where p.RoleID == 0
                            select p
                         ) on m.ID equals p.MenuID into po
                         from pox in po.DefaultIfEmpty()
                         select new PrivilegeMenu
                         {
                             ID = m.ID,
                             PrivilegeID = pox.ID,
                             Rowstatus = pox.Rowstatus,
                             Checked = pox.ID > 0 && pox.Rowstatus ? true : false,
                             RoleID = pox.RoleID,
                             MenuID = pox.MenuID,
                             MenuTitle = m.ParentID > 0 ? "|----- " + m.MenuTitle : m.MenuTitle,
                             ParentID = m.ParentID,
                             MenuSort = m.ParentID > 0 ? SqlFunctions.StringConvert((decimal)mpox.ParentID) + SqlFunctions.StringConvert((decimal)mpox.Sort) + SqlFunctions.StringConvert((decimal)m.Sort) : SqlFunctions.StringConvert((decimal)m.ParentID) + SqlFunctions.StringConvert((decimal)m.Sort),
                             AllowCreate = pox.ID > 0 ? pox.AllowCreate : false,
                             AllowUpdate = pox.ID > 0 ? pox.AllowUpdate : false,
                             AllowDelete = pox.ID > 0 ? pox.AllowDelete : false,
                             AllowApprove = pox.ID > 0 ? pox.AllowApprove : false,
                             CreatedBy = pox.CreatedBy,
                             CreatedOn = pox.CreatedOn,
                             LastModifiedBy = pox.LastModifiedBy,
                             LastModifiedOn = pox.LastModifiedOn
                         }).OrderBy(p => p.MenuSort);

            return _temp;
        }

        public static dynamic RetrieveByRoleID(int RoleID)
        {
            var _temp = (from m in db.Menus
                         join mp in (
                            from mp in db.Menus where mp.Rowstatus == true
                            select mp
                         ) on m.ParentID equals mp.ID into mpo
                         from mpox in mpo.DefaultIfEmpty()
                         join p in (
                            from p in db.Privilleges where p.RoleID == RoleID
                            select p
                         ) on m.ID equals p.MenuID into po
                         from pox in po.DefaultIfEmpty()
                         select new PrivilegeMenu
                         {
                             ID = m.ID,
                             PrivilegeID = pox.ID,
                             Rowstatus = pox.Rowstatus,
                             Checked = pox.ID > 0 && pox.Rowstatus ? true : false,
                             RoleID = pox.RoleID,
                             MenuID = pox.MenuID,
                             MenuTitle = m.ParentID > 0 ? "|----- " + m.MenuTitle : m.MenuTitle,
                             ParentID = m.ParentID,
                             MenuSort = m.ParentID > 0 ? SqlFunctions.StringConvert((decimal)mpox.ParentID) + SqlFunctions.StringConvert((decimal)mpox.Sort) + SqlFunctions.StringConvert((decimal)m.Sort) : SqlFunctions.StringConvert((decimal)m.ParentID) + SqlFunctions.StringConvert((decimal)m.Sort),
                             AllowCreate = pox.ID > 0 ? pox.AllowCreate : false,
                             AllowUpdate = pox.ID > 0 ? pox.AllowUpdate : false,
                             AllowDelete = pox.ID > 0 ? pox.AllowDelete : false,
                             AllowApprove = pox.ID > 0 ? pox.AllowApprove : false,
                             CreatedBy = pox.CreatedBy,
                             CreatedOn = pox.CreatedOn,
                             LastModifiedBy = pox.LastModifiedBy,
                             LastModifiedOn = pox.LastModifiedOn
                         }).OrderBy(p => p.MenuSort);

            return _temp;
        }

        public static void UpdatePrivilege(int RoleID, FormCollection collection, JobDescDBEntities1 dbt)
        {
            var pms = RetrieveByRoleID(RoleID);
            Privillege p;
            foreach (PrivilegeMenu pm in pms)
            {
                p = new Privillege();
                if (pm.PrivilegeID == null)
                {
                    p = dbt.Privilleges.CreateObject();
                    p.MenuID = pm.ID;
                    p.RoleID = RoleID;
                    p.Rowstatus = collection["id" + pm.ID.ToString()] != null ? true : false;
                    p.AllowCreate = collection["create" + pm.ID.ToString()] != null ? true : false;
                    p.AllowUpdate = collection["update" + pm.ID.ToString()] != null ? true : false;
                    p.AllowDelete = collection["delete" + pm.ID.ToString()] != null ? true : false;
                    p.AllowApprove = false;
                    p.CreatedBy = UserAuthenticated.isAuthenticated ? UserAuthenticated.authUser.FirstName : "DEFAULT";
                    p.CreatedOn = DateTime.Now;

                    dbt.Privilleges.AddObject(p);
                }
                else
                {
                    p = dbt.Privilleges.Single(pv => pv.ID == pm.PrivilegeID);
                    p.Rowstatus = collection["id" + pm.ID.ToString()] != null ? true : false;
                    p.AllowCreate = collection["create" + pm.ID.ToString()] != null ? true : false;
                    p.AllowUpdate = collection["update" + pm.ID.ToString()] != null ? true : false;
                    p.AllowDelete = collection["delete" + pm.ID.ToString()] != null ? true : false;
                    p.LastModifiedBy = UserAuthenticated.isAuthenticated ? UserAuthenticated.authUser.FirstName : "DEFAULT";
                    p.LastModifiedOn = DateTime.Now;

                    dbt.Privilleges.ApplyCurrentValues(p);
                }
            }
        }
    }

    public class PrivilegeMenu
    {
        public int ID { get; set; }
        public Nullable<int> PrivilegeID { get; set; }
        public Nullable<bool> Rowstatus { get; set; }
        public bool Checked { get; set; }
        public Nullable<int> RoleID { get; set; }
        public Nullable<int> MenuID { get; set; }
        public string MenuTitle { get; set; }
        public Nullable<int> ParentID { get; set; }
        public string MenuSort { get; set; }
        public bool AllowCreate { get; set; }
        public bool AllowUpdate { get; set; }
        public bool AllowDelete { get; set; }
        public bool AllowApprove { get; set; }
        public string CreatedBy { get; set; }
        public Nullable<DateTime> CreatedOn { get; set; }
        public string LastModifiedBy { get; set; }
        public Nullable<DateTime> LastModifiedOn { get; set; }
    }
}